<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        #box{
            width:100px;
            height:100px;
            background:red;
            position:absolute;
            left:100px;
            top:0px;
            /*这里不能带有margin*/
            /*margin: 100px;*/
        }
    </style>
</head>
<body>
    <button id="btn">开始运动</button>
    <div id="box"></div>
    <script type="text/javascript">
        var box = document.getElementById("box");
        var btn = document.getElementById("btn");
        btn.onclick = function () {
            startMove(box,400);
        }
        var timer = null;
        // target是终点，offsetLeft是起点
        function startMove(obj,target) {
            clearInterval(timer);
            var speed = (target-obj.offsetLeft)/10;
            //双向变化的速度
            speed>0?speed=Math.ceil(speed):speed=Math.floor(speed);

            timer = setInterval(function () {
                if(obj.offsetLeft==target){
                    clearInterval(timer);
                }else {
                    obj.style.left = obj.offsetLeft+speed+"px";
                }
                document.title = obj.style.left;
            },30);
        }
        
    </script>

</body>
</html>